package com.chendx.store.goods.netty;

import com.chendx.model.common.dtos.ResponseResult;
import com.chendx.store.common.constants.goods.GoodsConstants;
import com.chendx.store.goods.config.ApplicationContextProvider;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.HashMap;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by chendx on 2022/4/25.
 */
public class MyWebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {

    private static ObjectMapper MAPPER = new ObjectMapper();

    private StringRedisTemplate redisTemplate = ApplicationContextProvider.getApplicationContext().getBean(StringRedisTemplate.class);

    //从spring容器获取rabbitmq 监听容器
    private SimpleMessageListenerContainer sysNoticeContainer =
            (SimpleMessageListenerContainer) ApplicationContextProvider.getApplicationContext().getBean("sysNoticeContainer");

    private RabbitTemplate rabbitTemplate = ApplicationContextProvider.getApplicationContext().getBean(RabbitTemplate.class);

    public  static ConcurrentHashMap<String,Channel> userChannelMap = new ConcurrentHashMap<>();

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
        String userJSON = msg.text();
        String username = MAPPER.readTree(userJSON).get("username").asText();
        Channel channel = userChannelMap.get(username);
        if (channel == null){
            channel = ctx.channel();
            userChannelMap.put(username,channel);
        }
        if(channel.isActive() == false){
            channel = ctx.channel();
            userChannelMap.put(username,channel);
        }
        RabbitAdmin rabbitAdmin = new RabbitAdmin(rabbitTemplate);
        String queueName = "sku_focus_"+username;
        Properties queueProperties = rabbitAdmin.getQueueProperties(queueName);

        int noticeCount = 0;
        if (queueProperties != null){
            noticeCount = (int) queueProperties.get("QUEUE_MESSAGE_COUNT");
            redisTemplate.boundHashOps(GoodsConstants.NO_READ_GOODS_NOTICE).increment(username,noticeCount);
        }
        //封装返回的数据
        HashMap countMap = new HashMap();

        countMap.put("sysNoticeCount", noticeCount);
        ResponseResult responseResult = ResponseResult.okResult(countMap);
        channel.writeAndFlush(new TextWebSocketFrame(MAPPER.writeValueAsString(responseResult)));

        if (noticeCount > 0){
            rabbitAdmin.purgeQueue(queueName,true);
        }
        sysNoticeContainer.addQueueNames(queueName);
    }
}
